package com.mcxx.modules.system.service.impl;

import com.mcxx.modules.system.service.IndexPageService;
import com.mcxx.modules.BaseParam;
import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.miniSalvation.order.service.read.OrderReadService;
import com.mcxx.modules.temporary.order.service.read.TmpOrderReadService;
import com.mcxx.util.Constant;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@TaTransactional
public class IndexPageServiceImpl implements IndexPageService {

    @Autowired
    private OrderReadService orderReadService;
    @Autowired
    private TmpOrderReadService tmpOrderReadService;


    @Override
    public Map getOrderSummary(UserAccountVo accountVo) {
        HashMap<String, Integer> resultMap = new HashMap<>();

        if(null != accountVo){
            BaseParam param = new BaseParam();
            QueryParamUtil.setBaseParamValue(param, accountVo);


            List<Map> mapMiniList = orderReadService.countMiniBacklog(param);

            List<Map> mapList = orderReadService.countBacklog(param);
            int tempOrderCount =  tmpOrderReadService.countBacklog(param);


            resultMap.put("ls01", tempOrderCount);
            mapMiniList.forEach(countMap -> {
                String businessType = (String) countMap.get("businessType");
                String type = (String) countMap.get("type");
                String familyObjectType = (String) countMap.get("familyObjectType");
                Integer count = (Integer) countMap.get("count");
                if (Constant.BusinessCode.DIBAO.equals(businessType)) {
                    if ((Constant.OrderType.NEW.equals(type) || Constant.OrderType.REPUT.equals(type)) &&
                            Constant.MinimumLivingType.HOUSEHOLD.equals(familyObjectType)) {

                        resultMap.put("dbz01", count);
                    } else if((Constant.OrderType.NEW.equals(type) || Constant.OrderType.REPUT.equals(type)) &&
                            Constant.MinimumLivingType.SINGLE.equals(familyObjectType)) {


                        resultMap.put("dbd01", count);
                    } else if(Constant.OrderType.RECHECK.equals(type) &&
                            Constant.MinimumLivingType.HOUSEHOLD.equals(familyObjectType)) {


                        resultMap.put("dbz02", count);
                    } else if(Constant.OrderType.RECHECK.equals(type)  &&
                            Constant.MinimumLivingType.SINGLE.equals(familyObjectType)) {


                        resultMap.put("dbd02", count);
                    }
                }
            });
            mapList.forEach(countMap -> {
                String businessType = (String) countMap.get("businessType");
                String type = (String) countMap.get("type");
                Integer count = (Integer) countMap.get("count");
                if (Constant.BusinessCode.TEKUN.equals(businessType)) {

                    if (Constant.OrderType.NEW.equals(type) || Constant.OrderType.REPUT.equals(type)) {

                        resultMap.put("tk01", count);
                    } else {

                        resultMap.put("tk02", count);
                    }
                }
            });
        }

        return resultMap;
    }
}
